class RankListUI extends UIBase{

	private btn_close:BaseButton;
	private scroller_rank:eui.Scroller;
	private rank_list:eui.List;

	/**
	 * 我的排名
	 */
	private lb_rank:eui.Label;
	private img_icon:eui.Image;
	private lb_name:eui.Label;
	private lb_money:eui.Label;

	private btn_left:BaseButton;
	private btn_right:BaseButton;
	private rank_tittle:eui.Image;

	private _listDataArr:eui.ArrayCollection;
	private _tabIndex:number;
	private _crtPage:number;
	private _crtScrollerPosY:number;
	private main_group:eui.Group;

	public constructor(data?) {
        data.showBG = true;
		super('RankListView', data);
	}

	public init(){
		this.width = GlobalManager.instance().curWidth();
		this.height = GlobalManager.instance().curHeight();
		this.rank_list.itemRenderer = rankItem;
		this.fixIphoneX();

		this._tabIndex = 0;
		this._crtPage = 1;
		this.postSelfData();
	}

	public addEvent(){
		super.addEvent();
		this.btn_close.addTapHandler(this.btnClose, this);
		this.btn_left.addTapHandler(this.changeRank, this);
		this.btn_right.addTapHandler(this.changeRank, this);
		this.scroller_rank.addEventListener(eui.UIEvent.CHANGE_END, this.scrollerWorldChangeHandler, this);
	}

	//屏幕适配x
    protected fixIphoneX()
    {
		if(GlobalManager.instance().isIphonex())
		this.main_group.top = Math.floor(GlobalManager.instance().curHeight()/34);
    }

	protected changeRank()
	{
		if(this._tabIndex == 0)
		{
			this._tabIndex = 1;
			this.rank_tittle.source = 'rank_tittle_1_png';
		}
		else
		{
			this._tabIndex = 0;
			this.rank_tittle.source = 'rank_tittle_2_png';
		}
		this._crtPage = 1;
		this.rank_list.dataProvider = null;
		this._listDataArr = null;
		this.scroller_rank.stopAnimation();
		this.scroller_rank.viewport.scrollV = 0;
		RankManager.instance().worldUserData.add(this._tabIndex, null);
		this.postSelfData();
	}

	public postRankData()
	{		
		RankManager.instance().postRankData(this._crtPage, this._tabIndex, this.updateUI, this);
	}

	public postSelfData()
	{
		RankManager.instance().postSelfRankData(this._tabIndex, this.updateSelfRank, this);
	}

	public updateUI(data)
	{
		if(data)
		{
			let list:Array<any> = data['data'];

			if(!this._listDataArr){
				this._listDataArr = new eui.ArrayCollection();
			}

			list.sort(function(a, b)
			{
				return b["max_score"]  - a["max_score"];
			})

			for(let k in list)
			{
				let v = list[k];
				v.rank = parseInt(k) + 1; 
				v.type = data['type'];
			}

			this._listDataArr.replaceAll(list);
			if(!this.rank_list.dataProvider){
				this.rank_list.dataProvider = this._listDataArr;
			}
		}
	}

	protected updateSelfRank(data)
	{
		if(data)
		{
			let name = (data['nick_name'] == null) ? "你猜我是谁": data['nick_name'];
			let icon = data['avatar_url'] == null ? "normal_icon_jpg": data['avatar_url'];

			if(name == "")
			{
				name = "兰絮因果";
			}

			if(icon == "")
			{
				icon = "normal_icon_jpg";
			}
			
			let maxScore = data['max_score'];

			this.lb_rank.text = data.rank_id;

			this.lb_name.text = GameUtil.getNameSlice(name);
			this.img_icon.source = icon;
			let scoreStr = maxScore;
			if(maxScore >= 1000)
			{
				scoreStr = (Math.floor(maxScore / 1000) << 0) + 'k';
			}
			else if(maxScore >= 10000)
			{
				scoreStr = (Math.floor(maxScore / 10000) << 0) + 'w';
			}

			if(this._tabIndex == 0)
			{
				this.lb_money.text = '$'+scoreStr;
			}
			else
			{
				this.lb_money.text = scoreStr+'m';
			}
		}
		this.postRankData();
	}

	protected scrollerWorldChangeHandler(e:egret.Event)
	{
		//滑动判定分页请求
		let target:eui.IViewport = e.currentTarget.viewport;
		if(target.scrollV >= target.measuredHeight - target.height){
			// LogUtil.log('更新排行榜数据'); 
			this._crtScrollerPosY = target.scrollV;
			this._crtPage++;
			this.postRankData();
		}
	}

	public removeEvent(){
		super.removeEvent();
		this.btn_close.removeTapHandler();
		this.btn_left.removeTapHandler();
		this.btn_right.removeTapHandler();
		this.scroller_rank.removeEventListener(eui.UIEvent.CHANGE_END, this.scrollerWorldChangeHandler, this);
	}

	private btnClose(){
		this.remove(2);
	}

	public clear(){
		super.clear();
		RankManager.instance().worldUserData.add(this._tabIndex, null);
	}
}